//
//  YDAAccountSettingController.m
//  siyanone
//
//  Created by LK-IOS on 2018/3/26.
//  Copyright © 2018年 LK-News. All rights reserved.
//

#import "YDAAccountSettingController.h"
#import "YDAAccountSettingCell.h"
#import "YDAModifyPasswordController.h"//修改密码
#import "SYUploadAvaterManager.h"
#import "HXPhotoPicker.h"

@interface YDAAccountSettingController ()<UITableViewDelegate , UITableViewDataSource>

//tableView
@property (nonatomic , weak) UITableView *tableView;

//用户手机号
@property (nonatomic , copy) NSString *userPhoneNumber;

//选择照片
@property (strong, nonatomic) HXPhotoManager *manager;

@end

@implementation YDAAccountSettingController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = NumberColor(247, 247, 247);
    self.title = @"账户设置";
    //    [self tableView];
    
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self loadUserInformation];
}

///*********加载数据***********/////
-(void)loadUserInformation
{
    
    [[SYProgressHUD sharedInstance] showMBHudBackViewLoading];
    
    [BaseNetRequest sessionRequestWithparameters:nil method:GETMethod requstType:GetSettingInformationList WithSuccessBlock:^(NSDictionary *resultDict) {
        [[SYProgressHUD sharedInstance] dismissMBWithTime:0];
        //        DDLog(@"%@",resultDict)
        //        DDLog(@"%@",resultDict[@"message"])
        if ([resultDict[@"code"] integerValue] == 200) {
            
            //手机号注册的，必须有手机号
            self.userPhoneNumber = resultDict[@"result"][@"phone"];
            [self.tableView reloadData];
            
        }else{
            
            [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:@"信息加载出错，请重试~" isExistNav:YES];
            
            [self.navigationController popViewControllerAnimated:YES];
        }
    } failBlock:^(NSError *error) {
        //        DDLog(@"error = %@",error)
        [[SYProgressHUD sharedInstance] dismissMBWithTime:0];
        
        [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:@"信息加载出错，请重试~" isExistNav:YES];
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

/*******tableView********/
-(UITableView *)tableView
{
    if (_tableView == nil) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        [self.view addSubview:tableView];
        tableView.backgroundColor = NumberColor(247, 247, 247);
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.estimatedRowHeight = 0;
        tableView.estimatedSectionFooterHeight = 0;
        tableView.estimatedSectionHeaderHeight = 0;
        if (isIos11) {
            tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView = tableView;
    }
    return _tableView;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else{
        return 2;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return isPhoneX ? rateX812(50) : rate750(50);
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return isPhoneX ? rateX812(10) : rate750(10);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = NumberColor(247, 247, 247);
    return view;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString *phoneNumber = [NSString phoneNumberasterisk:self.userPhoneNumber];
    
    YDAAccountSettingCell *cell = [YDAAccountSettingCell cellWithTableView:tableView];
    //隐藏设置状态 section 2
    cell.setStatus.hidden = YES;
    
    //隐藏手机号
    if (indexPath.section == 1 && indexPath.row == 0) {
        cell.phoneLabel.hidden = NO;
        cell.phoneLabel.text = phoneNumber;
    }else{
        cell.phoneLabel.hidden = YES;
        
    }
    
    if (indexPath.section == 0) {
        if (indexPath.item == 0) {
            cell.imageName = @"mjbtwo-shezhi";
            cell.cateString = @"上传/更换头像";
            cell.accImage.hidden = NO;
        }
        
    }
    else if (indexPath.section == 1) {
        if (indexPath.item == 0) {
            cell.imageName = @"mjbtwo-shoujibangding";
            cell.cateString = @"手机绑定";
            cell.accImage.hidden = YES;
            
        }else{
            cell.imageName = @"mjbtwo-xiugaimima";
            cell.cateString = @"修改密码";
            cell.accImage.hidden = NO;
            
        }
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        //设置头像
        [self hx_presentAlbumListViewControllerWithManager:self.manager delegate:self];
    }
    
    else if (indexPath.section == 1) {
        if (indexPath.item == 0) {//手机绑定
        }else{//修改密码
            YDAModifyPasswordController *vc = [[YDAModifyPasswordController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
        
    }
}



- (void)albumListViewController:(HXAlbumListViewController *)albumListViewController didDoneAllList:(NSArray<HXPhotoModel *> *)allList photos:(NSArray<HXPhotoModel *> *)photoList videos:(NSArray<HXPhotoModel *> *)videoList original:(BOOL)original {
    if (photoList.count > 0) {
        HXPhotoModel *model = photoList.firstObject;
        //  [[PZProgressHUD sharedInstance] showMBHudWithSting:@"上传中~"];
        [SYUploadAvaterManager startUploadFinshWithImage:model.previewPhoto finish:^(NSString *url) {
            
            //  [[PZProgressHUD sharedInstance] dismissMBWithTime:0];
            
            
            if ([url isEqualToString:@"error"]) {
                
                [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:@"上传头像失败" isExistNav:YES];
            }
            else
            { [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeSuccess text:@"上传头像成功" isExistNav:YES];
                DDLog(@"头像成功地址====%@",url);
            }
        }];
        //TODO: FIX 上传照片
        
    }
}
- (HXPhotoManager *)manager {
    if (!_manager) {
        // __weak typeof(self) weakSelf = self;
        //HXPhotoManagerSelectedType 选择媒体资源的类型
        _manager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhoto];
        _manager.configuration.singleSelected = YES;// 是否为单选模式 默认 NO , 会自动过滤掉gif、livephoto
        _manager.configuration.albumListTableView = ^(UITableView *tableView) {
            //            NSSLog(@"%@",tableView);
        };
        
        _manager.configuration.themeColor = [UIColor whiteColor];
        
        _manager.configuration.deleteTemporaryPhoto = YES;//是佛删除拍摄的照片或视频
        _manager.configuration.saveSystemAblum = NO;//拍摄的视频或者相片是否保存到相册
        _manager.configuration.supportRotation = YES;//是否支持旋转
        _manager.configuration.cameraCellShowPreview = YES;//相机cell是否支持预览，默认为yes
        
        //设置弹出的相册选择界面导航栏的背景图片
        _manager.configuration.navigationTitleColor = [UIColor whiteColor];
        _manager.configuration.navigationBar = ^(UINavigationBar *navigationBar) {
            [navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"0E4483"]] forBarMetrics:UIBarMetricsDefault];
            navigationBar.barTintColor = [UIColor whiteColor];
            
        };
        _manager.configuration.statusBarStyle = UIStatusBarStyleLightContent;
        _manager.configuration.movableCropBox = YES;
        _manager.configuration.movableCropBoxEditSize = YES;
        _manager.configuration.movableCropBoxCustomRatio = CGPointMake(1, 1);//可移动裁剪框的宽高比
        
        
        
        
    }
    return _manager;
}


@end
